package com.zhenwgang.listen_books.service;

import com.zhenwgang.listen_books.data_object.AlbumDO;
import com.zhenwgang.listen_books.data_object.TrackDO;
import com.zhenwgang.listen_books.repository.AlbumRepo;
import com.zhenwgang.listen_books.repository.RelationRepo;
import com.zhenwgang.listen_books.repository.TrackRepo;
import com.zhenwgang.listen_books.util.Log;
import com.zhenwgang.listen_books.view_object.AlbumVO;
import com.zhenwgang.listen_books.view_object.BindView;
import com.zhenwgang.listen_books.view_object.UserVO;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Violet
 * @date 2022/07/12 20:02
 **/
public class AlbumService {
    private final AlbumRepo albumRepo = new AlbumRepo();
    private final TrackRepo trackRepo = new TrackRepo();
    private final RelationRepo relationRepo = new RelationRepo();

    public List<AlbumVO> listOfAlbum(UserVO userVO){
        int uid = userVO.uid;
        List<AlbumDO> albumDOList = albumRepo.selectListByUid(uid);
        List<AlbumVO> albumVOList = new ArrayList<>();
        for (AlbumDO albumDO : albumDOList){
            AlbumVO albumVO = new AlbumVO(albumDO.aid, albumDO.uid, albumDO.title, albumDO.cover, albumDO.state);
            albumVOList.add(albumVO);
        }
        return albumVOList;
    }

    public Object getBindInfo(UserVO userVO, int aid){
        AlbumDO albumDO = albumRepo.selectOneByUidAndAid(userVO.uid, aid);
        if (albumDO == null){
            throw new RuntimeException("aid不属于当前用户");
        }

        List<Integer> tidList = relationRepo.selectListByAid(aid);
        List<TrackDO> trackList = new ArrayList<>();

        if (!tidList.isEmpty()) {
            trackList = trackRepo.selectListByTid(tidList);
        }

        Log.println("音乐列表为：" + trackList);

        return new BindView(userVO, albumDO, trackList);
    }
}